package algotithm.leetcode.slidingwindow.test1056;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/23-9:36
 * @URL https://leetcode-cn.com/problems/grumpy-bookstore-owner/
 * @Title 1052. 爱生气的书店老板
 */
class Solution {

    public static void main(String[] args) {
        int[] customers = {1,0,1,2,1,1,7,5};
        int[] grumpy = {0,1,0,1,0,1,0,1};
        int X = 3;
        Solution solution = new Solution();
        int maxSatisfied = solution.maxSatisfied(customers, grumpy, X);
        System.out.println(maxSatisfied);
    }

    public int maxSatisfied(int[] customers, int[] grumpy, int X) {
        int total = 0;
        int n = customers.length;
        for (int i = 0; i < n; i++) {
            if (grumpy[i] == 0) {
                total += customers[i];
            }
        }
        int increase = 0;
        for (int i = 0; i < X; i++) {
            increase += customers[i] * grumpy[i];
        }
        int maxIncrease = increase;
        for (int i = X; i < n; i++) {
            increase = increase - customers[i - X] * grumpy[i - X] + customers[i] * grumpy[i];
            maxIncrease = Math.max(maxIncrease, increase);
        }
        return total + maxIncrease;
    }
}


